//
// Created by Administrator on 2022/9/30.
//
#include <stdio.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
int main(){

    int c_fd;
    struct sockaddr_in c_addr;
    char readBuf[128];
    char *msg = "msg from client myname is ta&to";

    //void *memset(void *s, int c, size_t n);
    memset(&c_addr,0,sizeof(struct sockaddr_in));

    //1.scoket
    //int socket(int domain, int type, int protocol);
    c_fd = socket(AF_INET,SOCK_STREAM,0);
    if(c_fd == -1){
        perror("socket");
        exit(-1);
    }

    c_addr.sin_family = AF_INET;
    c_addr.sin_port = htons(9527);
    inet_aton("127.0.0.1",&c_addr.sin_addr);

    //2.connect
    //int connect(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
    if(connect(c_fd,(struct sockaddr *)&c_addr,sizeof(struct sockaddr)) == -1){
        perror("connect");
        exit(-1);
    }

    //3.write
    //ssize_t write(int fd, const void *buf, size_t count);
    send(c_fd,msg,128,0);

    //4.read
    //ssize_t read(int fd, void *buf, size_t count);
    int n_read = recv(c_fd,readBuf,128,0);
    if(n_read == -1){
        perror("read");
    }else{
        printf("msg:%d,%s\n",n_read,readBuf);
    }

    return 0;
}

